package com.starbaba.controllog.service;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Qualifier;
import org.springframework.stereotype.Component;

import com.starbaba.core.service.CommonService;
import com.starbaba.core.utils.SpringContextManager;

@Component
public class ControlLogWriterService {

	private CommonService service;
	
	ControlLogWriterService() {
	}
	
	@Autowired @Qualifier("commonServiceImpl")
	protected void setService(CommonService service) {
		this.service = service;
	}
	
//	public void doWriteLog(JoinPoint pjp) throws Throwable {
//		System.err.println("拦截方法,进入日志记录");
//		Object target = pjp.getTarget();
//		// 拦截的方法名称
//		String methodName = pjp.getSignature().getName();
//		Object[] args = pjp.getArgs();
//		
//		// 拦截的参数类型
//		Class[] parameterTypes = ((MethodSignature) pjp.getSignature())
//				.getMethod().getParameterTypes();
//
//		Object object = null;
//		
//		// 获得被拦截的方法
//		Method method = null;
//		try {
//			method = target.getClass().getDeclaredMethod(methodName, parameterTypes);
//		} catch (NoSuchMethodException e) {
//			
//		}
//		if (method == null) {
//			Method[] methods = target.getClass().getMethods();
//			for (Method mthd : methods) {
//				if (mthd.getName().equals(methodName)) {
//					method = mthd;
//					break;
//				} 
//			}
//		}
//		// 判断是否包含自定义的注解
//		if (null != method&& method.isAnnotationPresent(LogAble.class) && target instanceof IActionLogAble) {
//			try {
//				LogAble myAnnotation = method.getAnnotation(LogAble.class);
//				IActionLogAble logAction = (IActionLogAble) target;
//				int operate = myAnnotation.operate();
//				UserVO userVO = logAction.getUserVO();
//				String username = userVO.getUsername();
//				CommonService service = getService();
//				// 根据id查询小分类名称
//				String actionId = logAction.getActionId();
//				String annotionActionId = myAnnotation.actionId();
//				if (annotionActionId.length() > 0) {
//					actionId = annotionActionId;
//				}
//				String smallCategorySql = "select lname, pcontrolid from car_management.sys_control where controlid = '"
//						+ actionId + "'";
//				List<?> smallCategoryList = service.doFindBySQL(smallCategorySql,null, null);
//				Object[] result = (Object[]) smallCategoryList.get(0);
//				String smallCategory =  (String) result[0];
//				String smallCategorypPcontrolid =  (String) result[1];
//				ControlLogCategory samllCategoryVO = new ControlLogCategory(
//						actionId, smallCategory);
//
//				// 截取actionId的前4位，为大分类的idzx
//				/*String bigCategoryId = actionId
//						.substring(0, 4);*/
//				//获取上一层的controlid和lname
//				String bigCategorySql = "select lname, pcontrolid from car_management.sys_control where controlid = '"
//						+ smallCategorypPcontrolid + "'";
//				List bigCategoryList = service.doFindBySQL(bigCategorySql, null, null);
//				Object[] result1 = (Object[]) bigCategoryList.get(0);
//				String bigCategory =  (String) result1[0];
//				String bigCategorypPcontrolid =  (String) result1[1];
//				ControlLogCategory bigCategoryVO = new ControlLogCategory(
//						smallCategorypPcontrolid, bigCategory);
//				
//				//获取最顶层的controlid和lname
//				String biggestCategorySql = "select lname, pcontrolid from car_management.sys_control where controlid = '"
//						+ bigCategorypPcontrolid + "'";
//				List biggestCategoryList = service.doFindBySQL(biggestCategorySql, null, null);
//				Object[] result2 = (Object[]) biggestCategoryList.get(0);
//				String biggestCategory =  (String) result2[0];
//				String biggestCategorypPcontrolid =  (String) result2[1];
//				ControlLogCategory biggestCategoryVO = new ControlLogCategory(
//						bigCategorypPcontrolid, biggestCategory);
//
//				
//				ArrayList<ControlLogCategory> productCategoryVOList = logAction.getProduct();
//				if (productCategoryVOList != null && !productCategoryVOList.isEmpty()) {
//					for (ControlLogCategory productCategoryVO : productCategoryVOList) {
//						ControlLogModel controlLogModel = new ControlLogModel(username,
//								new Date(), operate, bigCategoryVO, samllCategoryVO, biggestCategoryVO,
//								productCategoryVO);
//						ControlLogModelVO logModelVO = new ControlLogModelVO();
//						logModelVO.copyFromModel(controlLogModel);
//
//						Object ret = this.getService().doCreate(
//								ControlLogModelVO.class, logModelVO);
//						if (ret == null) {
//							System.err.println("日志记录失败");
//						}
//					}
//				} else {
//					System.err.println("日志记录失败");
//				}
//				
//			} catch (Exception e) {
//				System.err.println("日志记录失败");
//			}
//				
//		}
//		return;
//	}
	
	@SuppressWarnings("unused")
    private CommonService getService() {
		if (service == null) {
			this.service = SpringContextManager.getBean("commonServiceImpl");
		}
		return service;
	}
}
